package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketCollect;
import com.cskaoyan.market.db.domain.MarketCollectExample;
import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.db.mapper.MarketCollectMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 付诚
 * @Date: 2024/5/21 20:36
 * @Description:
 */
@Service
public class WxCollectServiceImpl implements WxCollectService {

    @Autowired
    private MarketCollectMapper marketCollectMapper;
    @Autowired
    private MarketGoodsMapper marketGoodsMapper;
    @Autowired
    private MarketUserMapper marketUserMapper;
    @Override
    public List<Object> list(Integer type, Integer page, Integer limit) {
        List<Object> list = new ArrayList<>();
        Integer id = getUserId();
        // 查询收藏
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        criteria.andUserIdEqualTo(id);
        criteria.andLogicalDeleted(false);

        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);

        // 查询商品
        marketCollects.forEach(marketCollect -> {
            Integer valueId = marketCollect.getValueId();
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(valueId);
            list.add(marketGoods);
        });

        PageHelper.startPage(page, limit);
        return list;
    }

    // 获取当前登录用户的id
    private static Integer getUserId() {
        Session session = SecurityUtils.getSubject().getSession();
        MarketUser user = (MarketUser) session.getAttribute("user");
        Integer id = user.getId();
        return id;
    }

    @Override
    public void addordelete(Integer type, Integer valueId) {
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        criteria.andLogicalDeleted(false);
        criteria.andValueIdEqualTo(valueId);
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);
        if (!marketCollects.isEmpty()) {
            // 删除收藏
            Integer id = marketCollects.get(0).getId();
            marketCollectMapper.deleteByPrimaryKey(id);
        }
        // 添加收藏
        MarketCollect marketCollect = new MarketCollect();
        marketCollect.setUserId(getUserId());
        marketCollect.setValueId(valueId);
        marketCollect.setType(type.byteValue());
        marketCollect.setAddTime(LocalDateTime.now());
        marketCollect.setUpdateTime(LocalDateTime.now());
        marketCollect.setDeleted(false);

        marketCollectMapper.insert(marketCollect);

    }
}
